package com.whfc.openapi.api.sys.service;

import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.entity.dto.login.MsUserLogin;
import com.whfc.fuum.dto.open.AccessCodeDTO;
import com.whfc.fuum.dto.open.AccessTokenDTO;
import com.whfc.fuum.dto.open.OpenApiClientDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.open.AccessTokenParam;
import com.whfc.fuum.param.open.OpenApiTokenParam;
import com.whfc.fuum.service.OpenApiService;
import com.whfc.fuum.service.SysUserService;
import com.whfc.hdc.dto.HdcAccessTokenDTO;
import com.whfc.hdc.params.HdcOpenApiTokenParam;
import com.whfc.hdc.service.HdcOpenAccessService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/10/21 11:49
 */
@Service
public class OpenApiTokenServiceImpl implements OpenApiTokenService {

    @DubboReference(interfaceClass = OpenApiService.class, version = "1.0.0")
    private OpenApiService openApiService;

    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0")
    private SysUserService sysUserService;

    @DubboReference(interfaceClass = HdcOpenAccessService.class, version = "1.0.0")
    private HdcOpenAccessService hdcOpenAccessService;

    @Override
    public AccessTokenDTO getAccessToken(AccessTokenParam param) {
        return openApiService.getAccessToken(param);
    }

    @Override
    public AccessTokenDTO getOpenApiToken(OpenApiTokenParam param) {
        return openApiService.getOpenApiToken(param);
    }

    @Override
    public OpenApiClientDTO validateOpenApiToken(String accessToken) {
        return openApiService.validateOpenApiToken(accessToken);
    }

    @Override
    public AccessCodeDTO getCode(Integer deptId, String username, String phone) {
        return openApiService.getCode(deptId, username, phone);
    }

    @Override
    public HdcAccessTokenDTO getHdcOpenApiToken(HdcOpenApiTokenParam param) {
        return hdcOpenAccessService.getOpenApiToken(param);
    }

    @Override
    public HdcAccessTokenDTO validateHdcOpenApiToken(String accessToken) {
        return hdcOpenAccessService.validateFshOpenApiToken(accessToken);
    }

    @Override
    public String getQiuyanAccessToken(String apiKey, String apiSecret) {
        return openApiService.getQiuyanAccessToken(apiKey, apiSecret);
    }

    @Override
    public void checkUser(MsUserLogin user) {
        SysUser sysUser = sysUserService.getUserByUsername(user.getUsername());
        if (sysUser != null) {
            String password = sysUser.getPassword();
            if (password.equals(user.getPassword())) {
                return;
            }
        }
        throw new BizException(ResultEnum.FAILURE.getCode(), "用户名密码错误");
    }

    @Override
    public Integer getDeptIdByAppKey(String appKey) {
        return openApiService.getDeptIdByAppKey(appKey);
    }
}
